Repository navigation
Fix Kafka to_binary() crash on missing datacontenttype or non-string attributes - #305
amdadulbari wants to merge 1 commit into
Conversation
d235ef3 to
4e081e9
Compare
|
@amdadulbari can you please update your PR and resolve merge conflicts? |
The Kafka binary conversion to_binary() read the optional datacontenttype attribute with event["datacontenttype"], raising KeyError for any event that does not set it (datacontenttype is optional in the CloudEvents spec). It also called .encode() directly on attribute values, raising AttributeError for spec-legal non-string extension attributes (e.g. integers). Read datacontenttype via .get() -- matching the sibling to_structured(), which already guards this attribute -- and stringify attribute values before encoding, matching the newer core Kafka binding. Add regression tests for both cases. Signed-off-by: Md. Amdadul Bari Imad <amdadulbari@gmail.com>
4e081e9 to
172322b
Compare
Done.. Rebased onto latest Re-ran everything locally after the rebase: full test suite passes (511), CI is showing |
EmiRZx
left a comment
There was a problem hiding this comment.
fix confirmed: main raises KeyError: 'datacontenttype' in both cases; here they work and tests pass. one thing: str(value) writes a boolean extension as True, while the spec's string form is true (the core bindings do the same).
Related Issue: #304
Type of change:
Description:
to_binary()incloudevents/v1/kafka/conversion.pyraised on two kinds of valid CloudEvents:KeyErrorwhen the optionaldatacontenttypeattribute was absent — it was read viaevent["datacontenttype"]. The siblingto_structured()in the same file already guards this withif "datacontenttype" in attrs:.AttributeErrorwhen an extension attribute had a non-string (e.g.int) value —value.encode("utf-8")was called directly. The newer core Kafka binding already stringifies withstr(attr_value).encode(...).This reads
datacontenttypevia.get()and stringifies attribute values before encoding, soto_binary()handles all valid events.Pre-submission checklist:
git commit --signoff.test_no_datacontenttype,test_non_string_extension_attribute— both fail before this change and pass after).CHANGELOG.md).pre-commit(ruff, ruff-format, mypy) and the Kafka test suite; all checks pass.